using NUnit.Framework;
using ProjectEuler.Core.Helpers;

namespace ProjectEuler.Core
{
    public class Problem41 : IProjectEulerProblem
    {
        protected long LargestPandigitalPrime()
        {
            // forget about 9-digit pandigitals, they are all divisible by 3
            // forget about 8-digit pandigitals, they are all divisible by 3 also
            // without those two qualifications, this algorithm runs *really* slow
            for(int i=7654321;i>=2;i--)
            {
                if(MathHelper.IsPrime(i))
                {
                    if(MathHelper.IsPandigital(i))
                    {
                        return i;
                    }
                }
            }
            return 0;
        }

        public int Number
        {
            get { return 41; }
        }

        public string Description
        {
            get { return "What is the largest n-digit pandigital prime that exists?"; }
        }

        public string Answer
        {
            get
            {
                return LargestPandigitalPrime().ToString();
            }
        }
    }

    [TestFixture]
    public class Problem41Tests : Problem41
    {
        [Test]
        public void Test_2143_is_pandigital_and_prime()
        {
            Assert.IsTrue(MathHelper.IsPrime(2143));
            Assert.IsTrue(MathHelper.IsPandigital(2143));
        }
    }
}